package com.example.day2.live.view;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.blankj.utilcode.util.SPUtils;
import com.example.day2.R;
import com.example.day2.entity.GiftResult;

import java.util.Timer;
import java.util.TimerTask;

public class BaoJiControl {
    private Context context;
    private GiftLayout giftLayout;
    private int count;
    private Timer timer = new Timer();
    private Handler handler = new Handler();
    private long lastTime;

    public BaoJiControl(Context context, GiftLayout giftLayout) {
        this.context = context;
        this.giftLayout = giftLayout;

        //启动定时器,超过三秒没有发送，隐藏
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                long now = System.currentTimeMillis();
                if (now - lastTime > 3000){
                    count = 0;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            giftLayout.setVisibility(View.GONE);
                        }
                    });
                }
            }
        },100,100);

    }
    //发送礼物
    public void  sendGift(GiftResult.DataBean dataBean){
        count++;
        if (count == 1){
            //设置数据
            show();

        }
        if (count>=1){
            giftLayout.setGift(dataBean);
            giftLayout.setCount(count);
            giftLayout.setUserName(SPUtils.getInstance().getString("username"));
            giftLayout.setUserImg(SPUtils.getInstance().getString("icon"));
        }

        lastTime = System.currentTimeMillis();
    }


    //显示暴击
    private void show(){
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.item_left_anim);
        giftLayout.setVisibility(View.VISIBLE);
        giftLayout.startAnimation(animation);
    }

}
